/*
 * Created on Feb 5, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package weather.net;

import java.io.BufferedReader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author Brian
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class Proxy {
    String port = null;

    String host = null;

    FileWriter fileWriter;

    FileReader fileReader;

    boolean useProxy;

    public Proxy() {
        try {
            BufferedReader input = null;
            input = new BufferedReader(new FileReader("proxyConfig"));
            String line = null;
            this.host = input.readLine();
            this.port = input.readLine();
            if (host.equals("no proxy")) {
                useProxy = false;
            } else {
                useProxy = true;
            }
        } catch (FileNotFoundException e) {
            useProxy = false;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void configureProxy(String host, String port) {
        this.host = host;
        this.port = port;

        if (host.equals("")) {
            useProxy = false;
            host = "no proxy";
        } else {
            useProxy = true;
        }

        try {
            fileWriter = new FileWriter("proxyConfig");
            fileWriter.write(host + "\n" + port);
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void enableProxy() {
        System.setProperty("proxySet", "true");
        System.setProperty("http.proxyHost", host);
        System.setProperty("http.proxyPort", port);
    }

    public void disableProxy() {
        System.setProperty("proxySet", "false");
        useProxy = false;
    }

    public boolean useProxy() {
        return useProxy;
    }

    public String getHost() {
        return host;
    }

    public String getPort() {
        return port;
    }

}
